package pub.cleangao.common.view;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;


@Schema(title = "数据返回")
@Data
public class R<T> implements Serializable {

    @Schema(title = "1:执行成功,消息有价值 0:执行成功 -1:不符合执行条件或错误结束 -3:无权限 -4:运行时异常")
    private int code = 0;

    @Schema(title = "提示消息")
    private String message = "SUCCESS";

    @Schema(title = "数据")
    private T data;

    public R() {
    }

    public static <T> R<T> ok() {
        R<T> rst = new R<>();
        rst.setCode(0);
        rst.setData(null);
        rst.setMessage("执行成功");
        return rst;
    }

    public static <T> R<T> ok(T data) {
        R<T> rst = new R<>();
        rst.setCode(0);
        rst.setData(data);
        rst.setMessage("执行成功");
        return rst;
    }


    public static <T> R<T> ok(T data, String message) {
        R<T> rst = new R<>();
        rst.setCode(0);
        rst.setData(data);
        rst.setMessage(message);
        return rst;
    }

    public static <T> R<T> okWithMessage(T data, String message) {
        R<T> rst = new R<>();
        rst.setCode(1);
        rst.setData(data);
        rst.setMessage(message);
        return rst;
    }

    public static <T> R<T> okMessageOnly(String message) {
        R<T> rst = new R<>();
        rst.setCode(0);
        rst.setData(null);
        rst.setMessage(message);
        return rst;
    }

    public static <T> R<T> error() {
        R<T> rst = new R<>();
        rst.setCode(-1);
        rst.setData(null);
        rst.setMessage("不符合执行条件或明确的执行期错误");
        return rst;
    }


    public static <T> R<T> error(T data) {
        R<T> rst = new R<>();
        rst.setCode(-1);
        rst.setData(data);
        rst.setMessage("不符合执行条件或明确的执行期错误");
        return rst;
    }

    public static <T> R<T> error(T data, String message) {
        R<T> rst = new R<>();
        rst.setCode(-1);
        rst.setData(data);
        rst.setMessage(message);
        return rst;
    }

    public static <T> R<T> errorMessageOnly(String message) {
        R<T> rst = new R<>();
        rst.setCode(-1);
        rst.setData(null);
        rst.setMessage(message);
        return rst;
    }


    public static <T> R<T> data(int code, T data, String message) {
        R<T> rst = new R<>();
        rst.setCode(code);
        rst.setData(data);
        rst.setMessage(message);
        return rst;
    }

    public static <T> R<T> data(CodeEnum type, T data) {
        R<T> rst = new R<>();
        rst.setCode(type.getValue());
        rst.setData(data);
        rst.setMessage(type.getDescription());
        return rst;
    }


    public static enum CodeEnum {

        SUCCESS_AND_TIP(1, "执行成功，携有价值提示消息"),
        SUCCESS(0, "执行成功"),
        ERROR(-1, "不符合执行条件或明确错误结束"),
        UNAUTHORIZED(-3, "无执行权限"),
        EXCEPTION(-4, "异常或未预料运行期错误"),
        ;

        final int value;
        final String description;

        CodeEnum(int value, String description) {
            this.value = value;
            this.description = description;
        }


        public int getValue() {
            return value;
        }

        public String getDescription() {
            return description;
        }

    }


}
